package com.qiuhui.springboot1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author: tian
 * @date: 2021/4/7  4:29 下午
 */
@RestController
@RequestMapping("/jdbc")
public class UserController {
    /**
     * Spring Boot 默认提供了数据源，默认提供了 org.springframework.jdbc.core.JdbcTemplate
     * JdbcTemplate 中会自己注入数据源，用于简化 JDBC操作
     */
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/list",method = {RequestMethod.GET})
    public List<Map<String,Object>> userList(){
        String query = "select * from poetry";
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(query);
        return maps;
    }

    @RequestMapping(value = "/update/{id}",method = {RequestMethod.GET})
    public String updateUser(@PathVariable("id") int id){
        String sql = "update poetry set title=?,author=? where id=" + id;
        Object[] obj = new Object[2];
        obj[0] = "赤壁赋";
        obj[1] = "苏轼";
        jdbcTemplate.update(sql,obj);
        return "update";
    }
}
